//
//  HYServerCollectionCell.m
//  HaiYue
//
//  Created by skyline on 2017/12/20.
//  Copyright © 2017年 skyline. All rights reserved.
//

#import "HYServerCollectionCell.h"
#import "HYKaifuModel.h"
NSString *const HYServerCollectionCelltemClick = @"HYServerCollectionCelltemClick";

@interface HYServerCollectionCell ()
@property (nonatomic, strong) HYImageView *IMG;
@property (nonatomic, strong) HYLabel *gameLBL;
@property (nonatomic, strong) HYLabel *serverLBL;
@property (nonatomic, strong) HYFillLabel *dateLBL;
@end
@implementation HYServerCollectionCell
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor whiteColor];
        self.selectedBackgroundView = [[HYView alloc] initWithColor:CELL_THEME_BACKGROUNDCOLOR(0.3)];
//        self.newLBL.text = @"新服";
    }
    return self;
}

- (HYImageView *)IMG
{
    if (!_IMG)
    {
        _IMG = [[HYImageView alloc]initWithImage:[UIImage imageNamed:@"1"] cornerRadius:CornerRadius / 2.0];
        [self addSubview:_IMG];
        [_IMG mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(GAP);
            make.centerY.equalTo(self).with.offset(- GAP * 2);
            make.size.mas_equalTo(CGSizeMake(HYServerCellIMG_width, HYServerCellIMG_width));
        }];
    }
    return _IMG;
}

- (HYLabel *)gameLBL
{
    if (!_gameLBL) {
        _gameLBL = [[HYLabel alloc]initWithText:@"" textColor:TEXT_BLACK_COLOR textFont:[UIFont systemFontOfSize:15]];
        [self addSubview:_gameLBL];
        [_gameLBL mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.IMG);
            make.left.equalTo(self.IMG.mas_right).with.offset(5);
        }];
    }
    return _gameLBL;
}

- (HYLabel *)serverLBL
{
    if (!_serverLBL)
    {
        _serverLBL = [[HYLabel alloc]initWithText:@"" textColor:ORANGE_COLOR textFont:[UIFont systemFontOfSize:13]];
        [self addSubview:_serverLBL];
        [_serverLBL mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.IMG);
            make.left.equalTo(self.IMG.mas_right).with.offset(5);
        }];
    }
    return _serverLBL;
}

- (HYFillLabel *)dateLBL
{
    if (!_dateLBL)
    {
        _dateLBL = [[HYFillLabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH / 2.0 - 1.5 * GAP, 25) typeOfshapeLayer:CCShapeLayerTypeEllipse attribute:@{HYTextColor:[UIColor whiteColor],HYTextFont:[UIFont systemFontOfSize:12],HYBorderColor:THEME_COLOR,HYLayerColor:[UIColor whiteColor]}];
        [self addSubview:_dateLBL];
        [_dateLBL mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self).with.offset(- GAP);
            make.centerX.equalTo(self);
            make.size.mas_equalTo(self.dateLBL.bounds.size);
        }];
    }
    return _dateLBL;
}

- (void)setModel:(HYKaifuModel *)model
{
    if (_model != model)
    {
        _model = model;
        self.IMG.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:_model.icon]]];
        self.gameLBL.text = _model.gamename;
        self.serverLBL.text = _model.server;
        self.dateLBL.text = [NSString stringWithFormat:@"开服时间:  %@",[_model.date substringWithRange:NSMakeRange(5, 11)]];
    }
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [UIView animateWithDuration:0.1 animations:^{
        self.selectedBackgroundView.transform = CGAffineTransformMakeScale(0.9, 0.9);
    } completion:^(BOOL finished) {
        self.selectedBackgroundView.transform = CGAffineTransformIdentity;
    }];
}
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
    [CCNotice post:HYServerCollectionCelltemClick obj:nil info:@{@"message":self.model.gameid}];
}
@end
